#!/bin/bash

#获取root权限密码
isNeedCheck=1
passwdCheck=0
passwd=""
if [ -n "$1" ]; then
    passwd="$1"
else
    echo -n "请输入root用户密码: "
    read passwd
fi

#验证密码正确性
until [ $passwdCheck = 1 ];do
    echo $passwd | sudo -S ls 1>/dev/null 2>/dev/null
    res=$?
    if [ $res -eq 0 ];then
        passwdCheck=1
        break
    fi
    passwd=""
    echo -n "密码错误，请重新输入root用户密码: "
    read passwd
done

# 1、更新源
echo "1、更新ROS源为清华源"
sudo bash -c 'echo "deb https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ bionic main" >> /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update

# 2、安装ROS
echo "2、安装ROS"
sudo apt-get install ros-melodic-desktop-full -y

# 3、安装其他的包
echo "2、安装ROS的其他包"
sudo apt install -y ros-melodic-gazebo-* ros-melodic-turtlebot3*
sudo apt install -y ros-melodic-usb-cam ros-melodic-usb-cam-dbgsym
sudo apt install -y ros-melodic-dynamixel-sdk
sudo apt install -y ros-melodic-cartographer
sudo apt install -y ros-melodic-cartographer-ros
sudo apt install -y ros-melodic-turtlesim
sudo apt install -y ros-melodic-gmapping ros-melodic-gmapping-dbgsym
sudo apt install -y ros-melodic-teleop-twist-keyboard
sudo apt install -y ros-melodic-navigation
sudo apt install -y ros-melodic-joint-state-publisher-gui ros-melodic-robot-state-publisher
sudo apt install -y ros-melodic-rgbd-launch ros-melodic-depthimage-to-laserscan

# 4、安装rosdep管理依赖
echo "4、安装rosdep"
echo "配置pip源"
## 配置pip源
mkdir -p ~/.pip
echo "[global] " > ~/.pip/pip.conf
echo "index-url = https://pypi.tuna.tsinghua.edu.cn/simple " >> ~/.pip/pip.conf
echo "[install] " >> ~/.pip/pip.conf
echo "trusted-host = https://pypi.tuna.tsinghua.edu.cn  # trusted-host " >> ~/.pip/pip.conf

echo "安装rosdep"
sudo apt install python3-pip -y
sudo pip3 install rosdepc
sudo rosdepc init  
rosdepc update  

# 5、配置环境变量
echo "5、配置环境变量"
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
echo "export GAZEBO_MODEL_PATH=/opt/ros/melodic/share/turtlebot3_gazebo/models" >> ~/.bashrc  
echo "export TURTLEBOT3_MODEL=burger" >> ~/.bashrc  
echo "export SVGA_VGPU10=0" >> ~/.bashrc  

# 6、软件配置
echo "6、进行软件配置"
git clone https://ghproxy.com/https://github.com/osrf/gazebo_models.git  
mkdir -p ~/.gazebo/models 
mv gazebo_models/ ~/.gazebo/models  
sudo chown smile:smile .ros -R

# sed -i 's/ignitionfuel/ignitionrobotics/g' ~/.ignition/fuel/config.yaml